package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.YueFanDetailTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.weidget.RoundImageView;
import com.xgkj.diyiketang.R;

import org.w3c.dom.Text;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by XCKJ_Paladowow on 2016/6/16.
 */
public class YueFanSJDetailActivity extends BaseActivity implements View.OnClickListener{
    private Context mContex;
    private TextView back;
    private Button mQiang;
    private String mActiveId;
    private TextView mTitle;
    private TextView mDescribtion;
    private TextView mStartTime;
    private TextView mLastTime;
    private TextView mRenshu;
    private TextView mShangQuan;
    private TextView mSexLimit;
    private String mUsrLogo;
    private RoundImageView userLogo;
    private TextView mZhuBoNick;
    private int yuefanDetailTaskId = -1;
    private HuoDongInfo hdinfo;
    private String mShangQuanIntent;
    private TextView mSign;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yuefanshangjiadetail);
        mContex = this;
        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        mActiveId = intent.getStringExtra("ACTIVEID");
        mUsrLogo = intent.getStringExtra("LOGO");
        mShangQuanIntent = intent.getStringExtra("SHANGQUAN");
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        userLogo = (RoundImageView) findViewById(R.id.head_img);
//        userLogo.setOnClickListener(this);
        if (mUsrLogo != null && mUsrLogo.length() > 0) {
            ImageCache.getInstance(mContext).loadBitmapToView(userLogo, mUsrLogo, null);
        }
        mZhuBoNick = (TextView) findViewById(R.id.zhubo_nick);
        mTitle = (TextView) findViewById(R.id.hd_title);
        mDescribtion = (TextView) findViewById(R.id.jianjie);
        mStartTime = (TextView) findViewById(R.id.kaishishijian);
        mLastTime = (TextView) findViewById(R.id.jiezhishijian);
        mSexLimit = (TextView) findViewById(R.id.shiyongrenqun);
        mRenshu = (TextView) findViewById(R.id.hd_renshu);
        mShangQuan = (TextView) findViewById(R.id.shangquan);
        mSign = (TextView) findViewById(R.id.zhubo_sign);
        mQiang = (Button) findViewById(R.id.baoming_btn);
        mQiang.setOnClickListener(this);
        getDetail();
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        yuefanDetailTaskId = -1;
//        getDetail();
//    }

    private void getDetail() {

        if (yuefanDetailTaskId != -1) {
            return;
        }
        YueFanDetailTask task = new YueFanDetailTask(mActiveId);
        task.addListener(yuefanDetailTask);
        yuefanDetailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }


    private TaskEventListener yuefanDetailTask = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {

            if (taskId == yuefanDetailTaskId) {
                hdinfo = (HuoDongInfo) data;

                mTitle.setText(hdinfo.getActivityTitle());
                mDescribtion.setText(hdinfo.getDescription());
                mStartTime.setText(hdinfo.getStartTime());
                mLastTime.setText(hdinfo.getLastTime());
                mRenshu.setText(hdinfo.getMaxCount());
                mShangQuan.setText(mShangQuanIntent);
                if (hdinfo.getSexLimit() != null) {
                    String sexNum = hdinfo.getSexLimit();
                    if (sexNum.equals("0")) {
                        mSexLimit.setText("无");
                    } else if (sexNum.equals("1")) {
                        mSexLimit.setText("限男性");
                    } else if (sexNum.equals("2")) {
                        mSexLimit.setText("限女性");
                    }
                }

                if (hdinfo.getNickName() != null) {
                    try {
                        mZhuBoNick.setText(URLDecoder.decode(hdinfo.getNickName(), "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                if (hdinfo.getUserSign() != null) {
                    try {
                        mSign.setText(URLDecoder.decode(hdinfo.getUserSign(), "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == yuefanDetailTaskId) {
                yuefanDetailTaskId = -1;
            }

        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.baoming_btn:
                Intent qIntent = new Intent(mContext,QiangYueFanActivity.class);
                qIntent.putExtra("ACTIVITYID",mActiveId);
                startActivity(qIntent);
                finish();
                break;
        }
    }
}
